package com.fr.web.core.service;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.base.XMLTools;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.UUID;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.json.JSONUtils;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.base.dav.DavXMLUtils;
import com.fr.base.dav.Env;
import com.fr.base.dav.EnvException;
import com.fr.base.dav.FileNode;
import com.fr.base.file.CacheManager;
import com.fr.base.file.ConfigManager;
import com.fr.base.file.DatasourceManager;
import com.fr.base.file.FaceManager;
import com.fr.base.file.FunctionManager;
import com.fr.base.xml.XMLReadable;
import com.fr.data.TableData;
import com.fr.data.core.DataUtils;
import com.fr.data.core.db.TableProcedure;
import com.fr.data.impl.DatabaseConnection;
import com.fr.data.impl.EmbeddedTableData;
import com.fr.privilege.authentication.Authentication;
import com.fr.privilege.authentication.UsernamePasswordAuthentication;
import com.fr.privilege.authority.Authority;
import com.fr.privilege.authority.Control;
import com.fr.privilege.manager.PrivilegeManager;
import com.fr.privilege.providers.AuthenticationProvider;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateWorkBook;
import com.fr.report.WorkBook;
import com.fr.report.io.ZipTemplateExporter;
import com.fr.report.io.ZipTemplateImporter;
import com.fr.report.parameter.Parameter;
import com.fr.report.web.ui.WidgetManager;
import com.fr.util.Utils;
import com.fr.web.core.WebUtils;
import com.fr.web.platform.PlatformControl;
import com.fr.web.platform.PlatformManager;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class RemoteDesignService extends NoSessionIDOPService
{
  private static RemoteDesignService REMOTEDESIGN_SERVICE = new RemoteDesignService();
  private static final UserEnv users = new UserEnv(null);
  private static final Object FILE_LOCK = new Object();
  public static final String INVALID_USER = "invalid user.";
  public static final String NO_SUCH_AUTHORITY = "No_Such_Authority";
  public static final String FILE_LOCKED = "file is locked by others.";
  public static final String RUNTIME_ERROR_PREFIX = "___FR_RUNTIME_ERROR_PREFIX___";
  public static final String NO_SUCH_RESOURCE = "No_such_resource";
  public static final String SIGN_IN = "design_sign_in";
  public static final String SIGN_OUT = "design_sign_out";
  public static final String GET_LOCK = "design_get_lock";
  public static final String RELEASE_LOCK = "design_release_lock";
  public static final String OPEN_REPORT = "design_open_report";
  public static final String SAVE_REPORT = "design_save_report";
  public static final String OPEN_RESOURCE = "design_open_resource";
  public static final String SAVE_RESOURCE = "design_save_resource";
  public static final String REPORT_EXIST = "design_report_exist";
  public static final String CREATE_FOLDER = "design_create_folder";
  public static final String CREATE_FILE = "design_create_file";
  public static final String FILE_EXISTS = "design_file_exists";
  public static final String FILE_INPUTSTREAM = "design_file_inputstream";
  public static final String LIST_FILE = "design_list_file";
  public static final String TEST_CON = "design_test_con";
  public static final String GET_TABLES = "design_get_tables";
  public static final String GET_TVP = "design_get_tvp";
  public static final String TABLEDATA_PARS = "design_td_pars";
  public static final String PREVIEW_TABLEDATA = "design_preview_td";
  public static final String REPORT_PARS = "design_report_pars";
  public static final String EXECUTE_REPORT = "design_execute_report";
  public static final String TEST_SERVER = "design_test_server_connection";
  public static final String GET_SCHEMA = "design_get_schema";
  private static final String ENTER = "\n";
  private static final String COLON = ":";

  public static RemoteDesignService getInstance()
  {
    return REMOTEDESIGN_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return ((VT4FR.REMOTE_DESIGN.support()) && (paramString.startsWith("design_")));
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = _dealWithRequest(paramHttpServletRequest, paramHttpServletResponse);
    if (str != null)
      try
      {
        PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse, "UTF-8");
        localPrintWriter.print("___FR_RUNTIME_ERROR_PREFIX___" + str);
        localPrintWriter.flush();
        localPrintWriter.close();
      }
      catch (Exception localException)
      {
        FRContext.getLogger().warning(localException.getMessage());
      }
  }

  private static String _dealWithRequest(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
  {
    String str1 = null;
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "op");
    if ("design_test_server_connection".equals(str2))
    {
      try
      {
        test_server_connnection(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException1)
      {
        FRContext.getLogger().warning(localException1.getMessage());
        str1 = createExceptionMessage("connect server error", localException1);
      }
      return str1;
    }
    String str3 = null;
    if (!("design_sign_in".equals(str2)))
    {
      String str4 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "id");
      str3 = is_userID_invalid(paramHttpServletResponse, str4);
      if (str3 != null)
        break label116;
      return "invalid user.";
    }
    try
    {
      sign_in(paramHttpServletRequest, paramHttpServletResponse);
    }
    catch (Exception localException2)
    {
      FRContext.getLogger().warning(localException2.getMessage());
      str1 = createExceptionMessage("Response encode error", localException2);
    }
    return str1;
    if ("design_sign_out".equals(str2))
      try
      {
        label116: sign_out(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException3)
      {
        FRContext.getLogger().warning(localException3.getMessage());
        str1 = createExceptionMessage("Response encode error", localException3);
      }
    else if ("design_open_report".equals(str2))
      try
      {
        read_report(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException4)
      {
        FRContext.getLogger().warning(localException4.getMessage());
        str1 = createExceptionMessage("Open report error", localException4);
      }
    else if ("design_save_report".equals(str2))
      try
      {
        save_report(paramHttpServletRequest, paramHttpServletResponse, str3);
      }
      catch (Exception localException5)
      {
        str1 = createExceptionMessage("Save report error", localException5);
        str1 = localException5.getMessage();
      }
    else if ("design_get_lock".equals(str2))
      try
      {
        get_lock(paramHttpServletRequest, paramHttpServletResponse, str3);
      }
      catch (Exception localException6)
      {
        FRContext.getLogger().warning(localException6.getMessage());
        str1 = createExceptionMessage("Get file lock error", localException6);
      }
    else if ("design_release_lock".equals(str2))
      try
      {
        release_lock(paramHttpServletRequest, paramHttpServletResponse, str3);
      }
      catch (Exception localException7)
      {
        FRContext.getLogger().warning(localException7.getMessage());
        str1 = createExceptionMessage("Release file lock error", localException7);
      }
    else if ("design_open_resource".equals(str2))
      try
      {
        read_resource(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException8)
      {
        FRContext.getLogger().warning(localException8.getMessage());
        str1 = createExceptionMessage("Open resource error", localException8);
      }
    else if ("design_save_resource".equals(str2))
      try
      {
        save_resource(paramHttpServletRequest, paramHttpServletResponse, str3);
      }
      catch (Exception localException9)
      {
        FRContext.getLogger().warning(localException9.getMessage());
        str1 = createExceptionMessage("Save resource error", localException9);
      }
    else if ("design_report_exist".equals(str2))
      try
      {
        is_report_exist(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException10)
      {
        FRContext.getLogger().warning(localException10.getMessage());
        str1 = createExceptionMessage("Check report existing error", localException10);
      }
    else if ("design_create_folder".equals(str2))
      try
      {
        create_folder(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException11)
      {
        FRContext.getLogger().warning(localException11.getMessage());
        str1 = createExceptionMessage("Create folder error", localException11);
      }
    else if ("design_create_file".equals(str2))
      try
      {
        create_file(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException12)
      {
        FRContext.getLogger().warning(localException12.getMessage());
        str1 = createExceptionMessage("Create file error", localException12);
      }
    else if ("design_file_exists".equals(str2))
      try
      {
        file_exists(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException13)
      {
        FRContext.getLogger().warning(localException13.getMessage());
        str1 = createExceptionMessage("Check file exists error", localException13);
      }
    else if ("design_file_inputstream".equals(str2))
      try
      {
        file_inputstream(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException14)
      {
        FRContext.getLogger().warning(localException14.getMessage());
        str1 = createExceptionMessage("Get file inputstreame error", localException14);
      }
    else if ("design_list_file".equals(str2))
      try
      {
        list_file(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException15)
      {
        FRContext.getLogger().warning(localException15.getMessage());
        str1 = createExceptionMessage("List files error", localException15);
      }
    else if ("design_test_con".equals(str2))
      try
      {
        testConnection(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException16)
      {
        FRContext.getLogger().warning(localException16.getMessage());
        str1 = createExceptionMessage("Test database connection error", localException16);
      }
    else if ("design_get_tables".equals(str2))
      try
      {
        getSQLTables(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException17)
      {
        FRContext.getLogger().warning(localException17.getMessage());
        str1 = createExceptionMessage("Get tables of the certain database error", localException17);
      }
    else if ("design_get_tvp".equals(str2))
      try
      {
        getTVP(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException18)
      {
        FRContext.getLogger().warning(localException18.getMessage());
        str1 = createExceptionMessage("Get tables of the certain database error", localException18);
      }
    else if ("design_get_schema".equals(str2))
      try
      {
        getSchema(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException19)
      {
        FRContext.getLogger().warning(localException19.getMessage());
        str1 = createExceptionMessage("Get schema of the certain database error", localException19);
      }
    else if ("design_td_pars".equals(str2))
      try
      {
        getTableDataParameters(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException20)
      {
        FRContext.getLogger().warning(localException20.getMessage());
        str1 = createExceptionMessage("Get parameters of tabledata error", localException20);
      }
    else if ("design_preview_td".equals(str2))
      try
      {
        previewTableData(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException21)
      {
        FRContext.getLogger().log(Level.WARNING, localException21.getMessage(), localException21);
        str1 = createExceptionMessage("Preview tabledata error", localException21);
      }
    else if ("design_report_pars".equals(str2))
      try
      {
        getReportParameters(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException22)
      {
        FRContext.getLogger().warning(localException22.getMessage());
        str1 = createExceptionMessage("Get parameters of report error", localException22);
      }
    else if ("design_execute_report".equals(str2))
      try
      {
        executeReport(paramHttpServletRequest, paramHttpServletResponse);
      }
      catch (Exception localException23)
      {
        FRContext.getLogger().warning(localException23.getMessage());
        str1 = createExceptionMessage("Execute report error", localException23);
      }
    return str1;
  }

  private static String createExceptionMessage(String paramString, Throwable paramThrowable)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(paramString).append(":\n");
    localStringBuffer.append("/------------------Remote Exception------------------").append("\n");
    StackTraceElement[] arrayOfStackTraceElement = paramThrowable.getStackTrace();
    if (!(ArrayUtils.isEmpty(arrayOfStackTraceElement)))
    {
      for (int i = 0; i < arrayOfStackTraceElement.length; ++i)
        localStringBuffer.append("\tat " + arrayOfStackTraceElement[i]).append("\n");
      Throwable localThrowable = paramThrowable.getCause();
      if (localThrowable != null)
        addThrowableMessage(localStringBuffer, localThrowable, arrayOfStackTraceElement);
    }
    localStringBuffer.append("------------------Remote Exception------------------/").append("\n");
    return localStringBuffer.toString();
  }

  private static void addThrowableMessage(StringBuffer paramStringBuffer, Throwable paramThrowable, StackTraceElement[] paramArrayOfStackTraceElement)
  {
    if ((paramStringBuffer == null) || (paramThrowable == null))
      return;
    StackTraceElement[] arrayOfStackTraceElement = paramThrowable.getStackTrace();
    if ((ArrayUtils.isEmpty(arrayOfStackTraceElement)) || (ArrayUtils.isEmpty(paramArrayOfStackTraceElement)))
      return;
    int i = arrayOfStackTraceElement.length - 1;
    for (int j = paramArrayOfStackTraceElement.length - 1; (i >= 0) && (j >= 0) && (arrayOfStackTraceElement[i].equals(paramArrayOfStackTraceElement[j])); --j)
      --i;
    int k = arrayOfStackTraceElement.length - 1 - i;
    paramStringBuffer.append("Caused by: " + paramThrowable.getMessage()).append(":\n");
    for (int l = 0; l <= i; ++l)
      paramStringBuffer.append("\tat " + arrayOfStackTraceElement[l]).append("\n");
    if (k != 0)
      paramStringBuffer.append("\t... " + k + " more").append("\n");
    Throwable localThrowable = paramThrowable.getCause();
    if (localThrowable != null)
      addThrowableMessage(paramStringBuffer, localThrowable, arrayOfStackTraceElement);
  }

  private static String is_userID_invalid(HttpServletResponse paramHttpServletResponse, String paramString)
  {
    return users.getUserName(paramString);
  }

  private static boolean is_file_writable(HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2)
    throws Exception
  {
    String str = users.getLockOwner("reportlets/" + paramString1);
    if ((str != null) && (!(ComparatorUtils.equals(str, paramString2))))
    {
      PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
      localPrintWriter.print("file is locked by others.");
      localPrintWriter.flush();
      localPrintWriter.close();
      return false;
    }
    return true;
  }

  private static void sign_in(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "user");
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "password");
    JSONObject localJSONObject = new JSONObject();
    try
    {
      String str3 = createUserID(paramHttpServletRequest, paramHttpServletResponse, str1, str2);
      if (str3 == null)
      {
        localJSONObject.put("exp", "invalid user or password.");
        FRContext.getLogger().warning("Failed to sign in----USER:[" + str1 + "].");
      }
      else
      {
        Env localEnv = FRContext.getCurrentEnv();
        localJSONObject.put("id", str3);
        FRContext.getLogger().info("Successfully sign in ----USER:[" + str1 + "].");
      }
    }
    catch (Exception localException)
    {
      localJSONObject.put("exp", localException.getMessage());
    }
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.println(localJSONObject.toString());
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void test_server_connnection(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "user");
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "password");
    String str3 = "";
    int i = 0;
    if (FRContext.getPrivilegeManager().usePrivilege())
    {
      localObject = FRContext.getPrivilegeManager();
      AuthenticationProvider localAuthenticationProvider = ((PrivilegeManager)localObject).getAuthenticationProvider();
      UsernamePasswordAuthentication localUsernamePasswordAuthentication = new UsernamePasswordAuthentication(str1, str2);
      try
      {
        localAuthenticationProvider.authenticate(localUsernamePasswordAuthentication);
        i = 1;
      }
      catch (Exception localException)
      {
      }
      if (i == 0)
        str3 = "invalid username or password.";
      else
        str3 = "true";
    }
    else
    {
      str3 = "true";
    }
    Object localObject = WebUtils.createPrintWriter(paramHttpServletResponse);
    ((PrintWriter)localObject).println(str3);
    ((PrintWriter)localObject).flush();
    ((PrintWriter)localObject).close();
  }

  private static String createUserID(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2)
    throws Exception
  {
    if (!(FRContext.getPrivilegeManager().usePrivilege()))
    {
      localObject1 = createRandomUserIDString();
      users.regUserName(paramString1, (String)localObject1);
      return localObject1;
    }
    Object localObject1 = FRContext.getPrivilegeManager();
    AuthenticationProvider localAuthenticationProvider = ((PrivilegeManager)localObject1).getAuthenticationProvider();
    if (localAuthenticationProvider == null)
      return null;
    UsernamePasswordAuthentication localUsernamePasswordAuthentication = new UsernamePasswordAuthentication(paramString1, paramString2);
    try
    {
      localAuthenticationProvider.authenticate(localUsernamePasswordAuthentication);
    }
    catch (Exception localException)
    {
      return null;
    }
    if (localUsernamePasswordAuthentication.isAuthenticated())
    {
      PlatformManager localPlatformManager = FRContext.getPlatformManager();
      PlatformControl localPlatformControl = localPlatformManager.getPlatformControl();
      if (localPlatformControl.getAuthoryryNameCount() > 0)
      {
        localObject2 = localUsernamePasswordAuthentication.getAuthorities();
        int i = 0;
        int j = localObject2.length;
        while (true)
        {
          if (i >= j)
            break label199;
          if (localPlatformControl.getAuthorityName(localObject2[i].getName()) != null)
          {
            String str = createRandomUserIDString();
            users.regUserName(paramString1, str);
            users.setAuthentication(localUsernamePasswordAuthentication);
            return str;
          }
          ++i;
        }
      }
      Object localObject2 = createRandomUserIDString();
      users.regUserName(paramString1, (String)localObject2);
      users.setAuthentication(localUsernamePasswordAuthentication);
      return localObject2;
    }
    label199: return ((String)(String)null);
  }

  private static String createRandomUserIDString()
  {
    return UUID.randomUUID().toString();
  }

  private static void sign_out(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "id");
    String str2 = users.getUserName(str1);
    JSONObject localJSONObject = new JSONObject();
    try
    {
      boolean bool = users.unRegUserID(str1);
      localJSONObject.put("res", Boolean.valueOf(bool));
      FRContext.getLogger().info("Successfully sign out ----USER:[" + str2 + "].");
    }
    catch (Exception localException)
    {
      localJSONObject.put("exp", localException.getMessage());
      FRContext.getLogger().warning("Failed to sign out ----USER:[" + str2 + "].");
    }
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    localPrintWriter.println(localJSONObject.toString());
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void testConnection(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ServletInputStream localServletInputStream = paramHttpServletRequest.getInputStream();
    DatabaseConnection localDatabaseConnection = DavXMLUtils.readXMLDatabaseConnection(localServletInputStream);
    boolean bool = FRContext.getCurrentEnv().testConnection(localDatabaseConnection);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse, "UTF-8");
    localPrintWriter.print(bool);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void getSchema(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ServletInputStream localServletInputStream = paramHttpServletRequest.getInputStream();
    DatabaseConnection localDatabaseConnection = DavXMLUtils.readXMLDatabaseConnection(localServletInputStream);
    String[] arrayOfString = FRContext.getCurrentEnv().getTableSchema(localDatabaseConnection);
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    DavXMLUtils.writeXMLFileSchema(arrayOfString, localServletOutputStream);
    localServletOutputStream.flush();
    localServletOutputStream.close();
  }

  private static void getSQLTables(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ServletInputStream localServletInputStream = paramHttpServletRequest.getInputStream();
    DatabaseConnection localDatabaseConnection = DavXMLUtils.readXMLDatabaseConnection(localServletInputStream);
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "__dbschema__");
    str = ((str == null) || (str.equals(""))) ? null : str;
    TableProcedure[] arrayOfTableProcedure = DataUtils.getDatabaseSQLTables(localDatabaseConnection, str);
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    DavXMLUtils.writeXMLFileSQLTable(arrayOfTableProcedure, localServletOutputStream);
    localServletOutputStream.flush();
    localServletOutputStream.close();
  }

  private static void getTVP(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ServletInputStream localServletInputStream = paramHttpServletRequest.getInputStream();
    DatabaseConnection localDatabaseConnection = DavXMLUtils.readXMLDatabaseConnection(localServletInputStream);
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "__type__");
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "__dbschema__");
    str2 = ((str2 == null) || (str2.equals(""))) ? null : str2;
    TableProcedure[] arrayOfTableProcedure = DataUtils.getTables(localDatabaseConnection, str1, str2, false);
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    DavXMLUtils.writeXMLFileSQLTable(arrayOfTableProcedure, localServletOutputStream);
    localServletOutputStream.flush();
    localServletOutputStream.close();
  }

  private static void getTableDataParameters(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ServletInputStream localServletInputStream = paramHttpServletRequest.getInputStream();
    TableData localTableData = DavXMLUtils.readXMLTableData(localServletInputStream);
    Parameter[] arrayOfParameter = FRContext.getCurrentEnv().getTableDataParameters(localTableData);
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    DavXMLUtils.writeXMLFileParameters(arrayOfParameter, localServletOutputStream);
    localServletOutputStream.flush();
    localServletOutputStream.close();
  }

  private static void previewTableData(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ServletInputStream localServletInputStream = paramHttpServletRequest.getInputStream();
    TableData localTableData = DavXMLUtils.readXMLTableData(localServletInputStream);
    int i = 200;
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "rowcount");
    if ((str != null) && (str.matches("\\d+")))
      i = Integer.parseInt(str);
    Map localMap = JSONUtils.jsonString2Map(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pars"));
    Object localObject = FRContext.getCurrentEnv().previewTableData(localTableData, localMap, i);
    localObject = EmbeddedTableData.embedify((TableData)localObject, i);
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    DavXMLUtils.writeXMLFileTableData((TableData)localObject, localServletOutputStream);
    localServletOutputStream.flush();
    localServletOutputStream.close();
  }

  private static void getReportParameters(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ZipTemplateImporter localZipTemplateImporter = new ZipTemplateImporter();
    WorkBook localWorkBook = localZipTemplateImporter.generateTemplate(paramHttpServletRequest.getInputStream());
    Parameter[] arrayOfParameter = FRContext.getCurrentEnv().getTemplateParameters(localWorkBook);
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    DavXMLUtils.writeXMLFileParameters(arrayOfParameter, localServletOutputStream);
    localServletOutputStream.flush();
    localServletOutputStream.close();
  }

  private static void executeReport(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    ZipTemplateImporter localZipTemplateImporter = new ZipTemplateImporter();
    WorkBook localWorkBook = localZipTemplateImporter.generateTemplate(paramHttpServletRequest.getInputStream());
    Map localMap = JSONUtils.jsonString2Map(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pars"));
    ResultWorkBook localResultWorkBook = localWorkBook.execute(localMap);
    ZipTemplateExporter localZipTemplateExporter = new ZipTemplateExporter();
    localZipTemplateExporter.export(paramHttpServletResponse.getOutputStream(), localResultWorkBook);
  }

  private static void create_folder(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "folder_path");
    Env localEnv = FRContext.getCurrentEnv();
    boolean bool = localEnv.createFolder(str);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse, "UTF-8");
    localPrintWriter.print(bool);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void create_file(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "file_path");
    Env localEnv = FRContext.getCurrentEnv();
    boolean bool = localEnv.createFile(str);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse, "UTF-8");
    localPrintWriter.print(bool);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void file_exists(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "file_path");
    boolean bool = FRContext.getCurrentEnv().fileExists(str);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse, "UTF-8");
    localPrintWriter.print(bool);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void file_inputstream(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "file_path");
    InputStream localInputStream = FRContext.getCurrentEnv().fileInputStream(str);
    ServletOutputStream localServletOutputStream = null;
    try
    {
      localServletOutputStream = paramHttpServletResponse.getOutputStream();
      if (localInputStream == null)
      {
        PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
        localPrintWriter.print("No_such_resource");
        localPrintWriter.flush();
        localPrintWriter.close();
      }
      else
      {
        Utils.copyBinaryTo(localInputStream, localServletOutputStream);
      }
    }
    finally
    {
      if (localServletOutputStream != null)
        localServletOutputStream.close();
    }
  }

  private static void list_file(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    int i;
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "file_path");
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "current_uid");
    if ((str1 == null) || (str2 == null))
      return;
    Env localEnv = FRContext.getCurrentEnv();
    FileNode[] arrayOfFileNode = localEnv.listFile(str1);
    synchronized (FILE_LOCK)
    {
      for (i = 0; i < arrayOfFileNode.length; ++i)
        arrayOfFileNode[i].setLock(users.getLockOwner(arrayOfFileNode[i].getEnvPath()));
    }
    ??? = paramHttpServletResponse.getOutputStream();
    DavXMLUtils.writeXMLFileNodes(arrayOfFileNode, (OutputStream)???);
    ((OutputStream)???).flush();
    ((OutputStream)???).close();
  }

  private static void get_lock(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    Env localEnv = FRContext.getCurrentEnv();
    ArrayList localArrayList = new ArrayList();
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pathes");
    JSONArray localJSONArray = new JSONArray(str1);
    int i = 0;
    int j = localJSONArray.length();
    while (i < j)
    {
      String str2 = localJSONArray.getString(i);
      localArrayList.addAll(Arrays.asList(localEnv.listFile(str2)));
      ++i;
    }
    synchronized (FILE_LOCK)
    {
      j = 1;
      int k = 0;
      int l = localArrayList.size();
      while (k < l)
      {
        FileNode localFileNode = (FileNode)localArrayList.get(k);
        String str3 = users.getLockOwner(localFileNode.getEnvPath());
        if ((str3 != null) && (!(ComparatorUtils.equals(str3, paramString))))
        {
          j = 0;
          break;
        }
        ++k;
      }
      JSONObject localJSONObject = new JSONObject();
      if (j != 0)
      {
        l = 0;
        int i1 = localArrayList.size();
        while (l < i1)
        {
          users.setLockOwner(((FileNode)localArrayList.get(l)).getEnvPath(), paramString);
          ++l;
        }
        localJSONObject.put("res", "success.");
      }
      else
      {
        localJSONObject.put("exp", "some file is locked by others.");
      }
      PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
      localPrintWriter.print(localJSONObject.toString());
      localPrintWriter.flush();
      localPrintWriter.close();
    }
  }

  private static void release_lock(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    Env localEnv = FRContext.getCurrentEnv();
    ArrayList localArrayList = new ArrayList();
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pathes");
    JSONArray localJSONArray = new JSONArray(str1);
    int i = 0;
    int j = localJSONArray.length();
    while (i < j)
    {
      String str2 = localJSONArray.getString(i);
      localArrayList.addAll(Arrays.asList(localEnv.listFile(str2)));
      ++i;
    }
    synchronized (FILE_LOCK)
    {
      j = 1;
      int k = 0;
      int l = localArrayList.size();
      while (k < l)
      {
        FileNode localFileNode = (FileNode)localArrayList.get(k);
        String str3 = users.getLockOwner(localFileNode.getEnvPath());
        if ((str3 != null) && (!(ComparatorUtils.equals(str3, paramString))))
        {
          j = 0;
          break;
        }
        ++k;
      }
      JSONObject localJSONObject = new JSONObject();
      if (j != 0)
      {
        l = 0;
        int i1 = localArrayList.size();
        while (l < i1)
        {
          users.setLockOwner(((FileNode)localArrayList.get(l)).getEnvPath(), null);
          ++l;
        }
        localJSONObject.put("res", "success.");
      }
      else
      {
        localJSONObject.put("exp", "some file is locked by others.");
      }
      PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
      localPrintWriter.print(localJSONObject.toString());
      localPrintWriter.flush();
      localPrintWriter.close();
    }
  }

  private static void is_report_exist(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "report_path");
    boolean bool = FRContext.getCurrentEnv().isTemplateExist(str);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse, "UTF-8");
    localPrintWriter.print(bool);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void read_report(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "report_path");
    Env localEnv = FRContext.getCurrentEnv();
    TemplateWorkBook localTemplateWorkBook = localEnv.readTemplate(str);
    ZipTemplateExporter localZipTemplateExporter = new ZipTemplateExporter();
    ServletOutputStream localServletOutputStream = paramHttpServletResponse.getOutputStream();
    localZipTemplateExporter.export(localServletOutputStream, localTemplateWorkBook);
    localServletOutputStream.flush();
    localServletOutputStream.close();
  }

  private static void save_report(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "report_path");
    synchronized (FILE_LOCK)
    {
      if (is_file_writable(paramHttpServletResponse, str, paramString))
        break label27;
      return;
      label27: if (!(FRContext.getCurrentEnv().isTemplateExist(str)))
        break label73;
      Object localObject2 = FRContext.getPrivilegeManager().getControl();
      if (((Control)localObject2).access(users.getAuthentication(), str, 3))
        break label73;
      throw new EnvException("No_Such_Authority");
      label73: localObject2 = FRContext.getCurrentEnv();
      ZipTemplateImporter localZipTemplateImporter = new ZipTemplateImporter();
      ((Env)localObject2).writeTemplate(localZipTemplateImporter.generateTemplate(paramHttpServletRequest.getInputStream()), str);
    }
  }

  public static void read_resource(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "resource");
    InputStream localInputStream = FRContext.getCurrentEnv().readResource(str);
    ServletOutputStream localServletOutputStream = null;
    try
    {
      localServletOutputStream = paramHttpServletResponse.getOutputStream();
      if (localInputStream == null)
      {
        PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
        localPrintWriter.print("No_such_resource");
        localPrintWriter.flush();
        localPrintWriter.close();
      }
      else
      {
        Utils.copyBinaryTo(localInputStream, localServletOutputStream);
      }
    }
    finally
    {
      if (localServletOutputStream != null)
        localServletOutputStream.close();
    }
  }

  private static void save_resource(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "resource");
    synchronized (FILE_LOCK)
    {
      if (is_file_writable(paramHttpServletResponse, BaseCoreUtils.pathJoin(new String[] { "resources", str }), paramString))
        break label42;
      return;
      label42: ServletInputStream localServletInputStream = paramHttpServletRequest.getInputStream();
      Env localEnv = FRContext.getCurrentEnv();
      Object localObject2 = null;
      if (!(FRContext.getDatasourceManager().fileName().equals(str)))
        break label99;
      Object localObject3 = new DatasourceManager();
      XMLTools.readInputStreamXML((XMLReadable)localObject3, localServletInputStream);
      FRContext.setDatasourceManager((DatasourceManager)localObject3);
      localObject2 = localObject3;
      break label339:
      label99: if (!(FRContext.getWidgetManager().fileName().equals(str)))
        break label140;
      localObject3 = new WidgetManager();
      XMLTools.readInputStreamXML((XMLReadable)localObject3, localServletInputStream);
      FRContext.setWidgetManager((WidgetManager)localObject3);
      localObject2 = localObject3;
      break label339:
      label140: if (!(FRContext.getFaceManager().fileName().equals(str)))
        break label181;
      localObject3 = new FaceManager();
      XMLTools.readInputStreamXML((XMLReadable)localObject3, localServletInputStream);
      FRContext.setFaceManager((FaceManager)localObject3);
      localObject2 = localObject3;
      break label339:
      label181: if (!(FRContext.getFunctionManager().fileName().equals(str)))
        break label222;
      localObject3 = new FunctionManager();
      XMLTools.readInputStreamXML((XMLReadable)localObject3, localServletInputStream);
      FRContext.setFunctionManager((FunctionManager)localObject3);
      localObject2 = localObject3;
      break label339:
      label222: if (!(FRContext.getConfigManager().fileName().equals(str)))
        break label263;
      localObject3 = new ConfigManager();
      XMLTools.readInputStreamXML((XMLReadable)localObject3, localServletInputStream);
      FRContext.setConfigManager((ConfigManager)localObject3);
      localObject2 = localObject3;
      break label339:
      label263: if (!(FRContext.getCacheManager().fileName().equals(str)))
        break label304;
      localObject3 = new CacheManager();
      XMLTools.readInputStreamXML((XMLReadable)localObject3, localServletInputStream);
      FRContext.setCacheManager((CacheManager)localObject3);
      localObject2 = localObject3;
      break label339:
      label304: FRContext.getLogger().log(Level.WARNING, "The XMLFileManager for resource :" + str + " may not exist!");
      label339: if (localObject2 == null)
        break label354;
      label354: localEnv.writeResource(localObject2);
    }
  }

  private static class UserInfo
  {
    private String userName;
    private List locks = Collections.EMPTY_LIST;

    UserInfo(String paramString)
    {
      this.userName = paramString;
    }

    public void lockFile(String paramString)
    {
      if (this.locks == Collections.EMPTY_LIST)
        this.locks = new ArrayList();
      this.locks.add(paramString);
    }

    public void unlockFile(String paramString)
    {
      this.locks.remove(paramString);
    }

    static String access$100(UserInfo paramUserInfo)
    {
      return paramUserInfo.userName;
    }

    static List access$200(UserInfo paramUserInfo)
    {
      return paramUserInfo.locks;
    }
  }

  private static class UserEnv
  {
    private final Map id_user_map;
    private final Map userName_user_map;
    private final Map lockingFiles;
    private Authentication authentication;

    private UserEnv()
    {
      this.id_user_map = new HashMap();
      this.userName_user_map = new HashMap();
      this.lockingFiles = new HashMap();
    }

    public Authentication getAuthentication()
    {
      return this.authentication;
    }

    public void setAuthentication(Authentication paramAuthentication)
    {
      this.authentication = paramAuthentication;
    }

    public void setLockOwner(String paramString1, String paramString2)
    {
      if (paramString1 == null)
        return;
      unlockFile(paramString1);
      RemoteDesignService.UserInfo localUserInfo = (RemoteDesignService.UserInfo)this.userName_user_map.get(paramString2);
      if (localUserInfo == null)
        return;
      paramString1 = paramString1.replaceAll("\\\\", "/");
      this.lockingFiles.put(paramString1, localUserInfo);
      localUserInfo.lockFile(paramString1);
    }

    public void unlockFile(String paramString)
    {
      if (paramString == null)
        return;
      paramString = paramString.replaceAll("\\\\", "/");
      RemoteDesignService.UserInfo localUserInfo = (RemoteDesignService.UserInfo)this.lockingFiles.get(paramString);
      if (localUserInfo != null)
      {
        localUserInfo.unlockFile(paramString);
        this.lockingFiles.remove(paramString);
      }
    }

    public String getLockOwner(String paramString)
    {
      if (paramString == null)
        return null;
      paramString = paramString.replaceAll("\\\\", "/");
      RemoteDesignService.UserInfo localUserInfo = (RemoteDesignService.UserInfo)this.lockingFiles.get(paramString);
      return ((localUserInfo == null) ? null : RemoteDesignService.UserInfo.access$100(localUserInfo));
    }

    public String getUserName(String paramString)
    {
      if (paramString == null)
        return null;
      RemoteDesignService.UserInfo localUserInfo = (RemoteDesignService.UserInfo)this.id_user_map.get(paramString);
      return ((localUserInfo == null) ? null : RemoteDesignService.UserInfo.access$100(localUserInfo));
    }

    public void regUserName(String paramString1, String paramString2)
    {
      if ((paramString1 == null) || (paramString2 == null))
        return;
      RemoteDesignService.UserInfo localUserInfo = new RemoteDesignService.UserInfo(paramString1);
      this.id_user_map.put(paramString2, localUserInfo);
      this.userName_user_map.put(paramString1, localUserInfo);
    }

    public boolean unRegUserID(String paramString)
    {
      if (paramString == null)
        return true;
      RemoteDesignService.UserInfo localUserInfo = (RemoteDesignService.UserInfo)this.id_user_map.get(paramString);
      if (localUserInfo == null)
        return true;
      String[] arrayOfString = (String[])RemoteDesignService.UserInfo.access$200(localUserInfo).toArray(new String[RemoteDesignService.UserInfo.access$200(localUserInfo).size()]);
      for (int i = 0; i < arrayOfString.length; ++i)
        unlockFile(arrayOfString[i]);
      this.id_user_map.remove(paramString);
      this.userName_user_map.remove(RemoteDesignService.UserInfo.access$100(localUserInfo));
      return true;
    }

    public boolean isUserRegistrated(String paramString)
    {
      return (this.userName_user_map.get(paramString) != null);
    }

    UserEnv(RemoteDesignService.1 param1)
    {
    }
  }
}